var app = getApp()
Page({
  data:{
    banner: [
     
    ],
    tags: [],
    icons: [],
    indicatorDots: true,
    autoplay: true,
    circular: true,
    vertical: true,
    interval: 5000,
    duration: 1000,
    firstact: 0,
    btm_line:false,
    pages:1,
    navlist:[
      {
        title: '当季热门',
        id: 0
      },{
        title: '周边景点',
        id: 1
      }
    ],
    addr: {},
    sight:[],
    addrlist: [],
    activeIndex:0,
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    that.get_local_position();//定位
    get_banner(this);//banner
    get_recommendation(this);//栏目
    
    
  },
  get_local_position:function(e){
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(res)
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy;
        that.setData({
          addr:{
            latitude: latitude,
            longitude: longitude
          }
        })
        wx.getStorage({
          key: 'jl_user',
          success: function (res) {
            console.log(res)
            that.setData({
              user: res.data
            })
            if (res.data.token) {
              guess_youlike(that, res.data.token)//猜你喜欢
            }
          },
          fail:function(err){
            guess_youlike(that)//猜你喜欢
          }
        })
       
        get_information(that, 1);//当季热门
      }
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示 监听是否刚登陆需要刷新数据
    if (global.reloadFlag==0){
      global.reloadFlag=1;
      this.onLoad();
    }
    

  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  // 列表点击事件
  gotodetail:function(e){
    var tid = e.currentTarget.dataset.id
    // console.log(tid)
    wx.navigateTo({
      url: '../detail/detail?id='+ tid,
    })
  },
  // banner点击事件
  bannertapbanner: function (e) {//banner
    var type = e.currentTarget.dataset.type
    var id = e.currentTarget.dataset.id
    var url = e.currentTarget.dataset.url
    var name = e.currentTarget.dataset.name
    if (type == 1) {
      wx.navigateTo({
        url: '../detail/detail?id=' + url,
      })
    }
    if (type == 2) {
      wx.navigateTo({
        url: '../category/category?type=' + url + "&name=" + name
      })

    }
    if (type == 3) {
      wx.navigateTo({
        url: "../webview/webview?url=" + url,
      })
    }
  },
  navlist:function(e){
    var that = this;
    this.setData({
      activeIndex: e.currentTarget.id,
      firstact: -1,
      pages:1,
    });
    console.log(e)
    if (e.currentTarget.id==0){
      get_information(that, 1);
    }else{
      get_nearby_attractions(that, 1)
    }
   
  },
  // 跳转全部景点
  allsights:function(e){
    wx.switchTab({
      url: '../ticket/ticket',
    })
  },
  icon:function(e){
    // console.log(e.currentTarget.dataset.type)
    var type = e.currentTarget.dataset.type
    var id = e.currentTarget.dataset.id
    var url = e.currentTarget.dataset.url
    var name = e.currentTarget.dataset.name
    if(type == 1){
      wx.navigateTo({
        url: '../detail/detail?id=' + url,
      })
    }
    if (type == 2) {
      wx.navigateTo({
        url: '../category/category?type=' + url + "&name=" + name
      })
     
    }
    if (type == 3) {
      wx.navigateTo({
        url: "../webview/webview?url="+url,
      })
    }
  },
  scroll_tobtm:function(e){
    console.log(e)
    var tabindex = this.data.activeIndex
    var pages = this.data.pages
    var that = this
    console.log('到底啦')
    if (tabindex==0){
      get_information(that, pages)
    }else{
      get_nearby_attractions(that, pages)
    }
  },
  onShareAppMessage:function(e){
    
  }
})

// 获取bannner
function get_banner(that){
  app.util.request({
    'url': 'entry/wxapp/benner',
    'cachetime': '30',
    success: function (res) {
      // console.log(res.data)
      if (res.data.status){
        var ret = res.data;
        var banners =[];
        
        for (var i = 0; i < ret.data.length;i++){
          // console.log(i)
          banners.push({
            url: ret.data[i].url,
            logo: ret.data[i].logo, 
            type: ret.data[i].type, 
            id: ret.data[i].id, 
            name: ret.data[i].name
          })
          // console.log(banners)
        } 
        that.setData({
          banner: banners
        })
      }
    }
  });
}

// 推荐位，栏目位
function get_recommendation(that){
  app.util.request({
    'url': 'entry/wxapp/recommendation',
    'cachetime': '30',
    success: function (res) {
      console.log(res.data)
      if (res.data.status) {
        var ret = res.data;
        var icon = []
        icon = ret.data
        that.setData({
          icons: icon
        })
      }
    }
  });
}

// 根据地理位置获取当季热门景点
function get_information(that, pages){
  app.util.request({
    'url': 'entry/wxapp/information',
    // 'cachetime': '30',
    data:{
      page: pages,
      longitude: that.data.addr.longitude,
      latitude: that.data.addr.latitude,
      
    },
    success: function (res) {
      if (res.data.status) {
        console.log(res.data)
        var ret = res.data;
        if(ret.data.length>0){
          that.setData({
            pages: pages + 1,
          })
        }
        if(pages>1){
          var list = that.data.sight;
          list = list.concat(ret.data);
          that.setData({
            sight: list
          })
          if (ret.data.length == 0) {
            that.setData({
              btm_line: true,
            })
            setTimeout(function () {
              that.setData({
                btm_line: false,
              })
            }, 2000)
            app.util.message(res.data.msg, '', 'err_msg')
          }
        }else{
          that.setData({
            sight: ret.data,
          })
        }

        
      }else{
        console.log(res)
        app.util.message(res.data.msg, '', 'err_msg')
      }
    }
  });
}
//根据经纬度获取周边景点 距离排序
function get_nearby_attractions(that, pages) {
  // console.log(that.data.addr)
  app.util.request({
    'url': 'entry/wxapp/nearby_attractions',
    'cachetime': '30',
    data: {
      page:pages,
      longitude: that.data.addr.longitude,
      latitude: that.data.addr.latitude,
    },
    success: function (res) {
      console.log(res)
      if (res.data.status) {
        var ret = res.data;
        if (ret.data.length > 0) {
          that.setData({
            pages: pages + 1,
          })
        }
        if(pages>1){
          var list = that.data.sight;
          list = list.concat(ret.data);
          that.setData({
            sight: list,
            pages: pages+1,
          })
          if(ret.data.length==0){
            that.setData({
              btm_line: true,
            })
            setTimeout(function(){
              that.setData({
                btm_line: false,
              })
            },2000)
            app.util.message(res.data.msg, '', 'err_msg')
          }
        }else{
          that.setData({
            sight: ret.data
          })
        }
        
      }else{
        console.log(res)
        app.util.message(res.data.msg, '', 'err_msg')

      }
    }
  });
}
// 猜你喜欢
function guess_youlike(that,token){
  var params ={
    longitude: that.data.addr.longitude,
    latitude: that.data.addr.latitude,
  }
  if (token){
    params.token = token;
  }
  app.util.request({
    'url': 'entry/wxapp/recommended_spots',
    // 'cachetime': '30',
    data: params,
    success: function (res) {
      console.log(res)
      if (res.data.status) {
        var ret = res.data;
        that.setData({
          likelist: ret.data
        })
      }
    }
  });
}


// 记录浏览记录
function record_browse(token,tid){
  app.util.request({
    url:'entry/wxapp/recording',
    data:{
      stock_token:token,
      tid:tid,
    },
    success:function(res){
      var ret = res.data;
      console.log(ret)
    }
  })
}